package com.zjitc.controller;

import com.zjitc.dao.UserDao;
import com.zjitc.model.User;
import com.zjitc.service.UserService;
import com.zjitc.service.impl.UserServiceImpl;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import redis.clients.jedis.Jedis;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/4/17 15:12
 * @description:
 */
@Controller
@RequestMapping("/user")
public class UserController {

  @Autowired
  private Jedis jedis;

  @Autowired
  private UserService userService;

    @RequestMapping("/login")
    private ModelAndView login(
      @RequestParam(name = "username") String username,
      @RequestParam(name = "password") String password,
      HttpSession session){
      User user=userService.login(username,password);
      if (user==null||user.getState().equals(0)){
        return new ModelAndView("redirect:/user/login.jsp");
      }else {
        session.setAttribute("user",user);
        return new ModelAndView("redirect:/index.do");
      }
    }

    @RequestMapping("/register")
    private ModelAndView regist(User user){
        int count=userService.regist(user);
        if (count==1){
          return new ModelAndView("redirect:/index.do");
        }else {
          return new ModelAndView("redirect:/user/regist.jsp");
        }
    }

   /* @RequestMapping("/activeCode")
    private ModelAndView activeCode(){
    }*/

    @RequestMapping("/logout")
    private ModelAndView logout(HttpSession session){
      session.removeAttribute("user");
      return new ModelAndView("redirect:/index.do");
    }

    @RequestMapping("/myCart")
    private ModelAndView myCart(HttpSession session){
      if (session.getAttribute("user")!=null){
        return new ModelAndView("redirect:/user/cart.jsp");
      }else {
        return new ModelAndView("redirect:/user/login.jsp");
      }
    }
}
